<?php

namespace app\api\controller;

class V1 extends ApiBase  {

	/**
     * 发送WD
     */
    public function transfer(){
        try{
            $from = $this->_post('from');
            $to_address = $this->_post('to');
            if(!$to_address) return $this->failJSON('请输入钱包地址');
            $amount = $this->_post('amount');
            if($amount<=0) return $this->failJSON('请输入正确的金额');
            $api = new \app\api\utils\LotusApi;
            $res = $api->WalletValidateAddress($from);
            if(!isset($res['result'])){
                return $this->failJSON($res['error']['message']);
            }
            $res = $api->WalletValidateAddress($to_address);
            if(!isset($res['result'])){
                return $this->failJSON($res['error']['message']);
            }
            $balance = $api->WalletBalance($from);
            $address_balance = 0;
            if($balance['result']>0){
                $address_balance = bcdiv($balance['result']/bcpow(10, 18), 1,8) ;
            }
            if($amount>$address_balance){
                return $this->failJSON('余额不足无法转账');
            }
            $GasLimit = 6000000;
            $amount = $amount*bcpow(10,18);
            $amount = $this->sctonum($amount,0);
            $res = $api->MpoolPushMessage($from,$to_address,(string)$amount,$GasLimit);
            if(isset($res['result'])){
                return $this->successJSON($res['result']);
            }else{
                return $this->failJSON('转账失败');
            }
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    private function sctonum($num, $double = 5){
        if (false !== stripos((string)$num, "E")) {
            $a = explode("e", strtolower((string)$num));
            $dd = bcpow((string)10, (string)$a[1], $double);
            $b=bcmul($a[0],$dd, $double);
            $b=$b?$b:0;
            return $b;
        }else{
            return $num;
        }
    }


    /**
     * 创建地址
     */
    public function newAddress(){
        try{
		    $api = new \app\api\utils\LotusApi;
	        $address = $api->WalletNew();
	        if(!isset($address['result'])){
	            return $this->failJSON('创建钱包失败');
	        }
	        return $this->successJSON(['address'=>$address['result']]);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }



    /**
     * 交易记录根据地址查询
     */
    public function transactionRecordsByAddress(){
        try{
            $address = $this->_post('address');
            $page = $this->_post('page')>0?$this->_post('page'):0;
            $rows = $this->_post('rows')==''?50:$this->_post('rows');
            $url = 'https://main.worlddb.io/index/index/addressMessageList';
			$req = $url.'?address='.$address.'&page='.$page.'&rows='.$rows;
			$opts = array(
				CURLOPT_TIMEOUT => 30,
				CURLOPT_RETURNTRANSFER => 1,
				CURLOPT_SSL_VERIFYPEER => false,
				CURLOPT_SSL_VERIFYHOST => false,
				CURLOPT_URL => $req,
			);
			$ch = curl_init();
			curl_setopt_array($ch, $opts);
			$data = curl_exec($ch);
			curl_close($ch);
			$res = json_decode($data, true);
			if($res['success']){
		        return $this->successJSON($res['data']);
			}else{
		        return $this->failJSON($res['message']);
			}
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }


    }


    /**
     * 交易记录根据CID查询
     */
    public function transactionRecordsByCid(){
		try{
            $cid = $this->_post('cid');
	    	$api = new \app\api\utils\LotusApi;
	        $address = $api->ChainGetMessage($cid);
	        if(isset($address['result'])){
	        	$address['result']['amount'] = $address['result']['Value']/bcpow(10, 18);
	        }
		    return $this->successJSON($address['result']);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }


}
